﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using The_Last_Knights.GUI;

namespace The_Last_Knights.Helper.Xml
{
    class ButtonReader: ReaderEntity
    {        
        public ButtonReader()
        {
        }

        public override object Read(XElement element, string ID, int level)
        {
            string fontName = null;
            string text = null;
            try
            {
                //Co nhung button khong co text
                fontName = element.Attribute(XName.Get(@"Font")).Value;
                text = element.Attribute(XName.Get(@"Text")).Value;        
            }
            catch(Exception){}
                   
            Vector2[] vector2s = new Vector2[2];
            int index = 0;
            string direction = null;
            var defs = element.Descendants(XName.Get(@"Element"));
            foreach (var xElement in defs)
            {
                string name = xElement.Attribute(XName.Get(@"ID")).Value;
                string id = XmlReader.TranslateID(name);
                string type = xElement.Attribute(XName.Get(@"Type")).Value;
                int lev = int.Parse(xElement.Attribute(XName.Get(@"Level")).Value);
                
                direction = null;
                try
                {
                    direction = element.Attribute(XName.Get(@"Direction")).Value;
                }
                catch(Exception){}                
                if (lev == level + 1)
                    switch (type)
                    {
                        case "Vector2":
                            vector2s[index] = (Vector2) Factory.Read(xElement, id, type, lev);
                            index++;
                            break;                            
                    }                    
            }

            string normalDir = null, hoverDir = null, pushDir = null;
            normalDir = direction + "/normal";
            hoverDir = direction + "/hover";
            pushDir = direction + "/push";
            string Type = element.Attribute(XName.Get(@"Type")).Value;

            Text label = null;
            if (fontName != null)
                label = new Text(Game, text, fontName);
            if (Type == "NormalButton")
                return new NormalButton(Game, label, normalDir, hoverDir, pushDir, ID, 
                                        vector2s[0], vector2s[1], null);
            if (Type == "PushButton")
                return new PushButton(Game, label, normalDir, hoverDir, pushDir, ID,
                                        vector2s[0], vector2s[1], null);            
            if (Type == "DisableButton")
                return new DisableButton(Game, label, normalDir, hoverDir, pushDir, ID,
                                        vector2s[0], vector2s[1], null);
            return null;
        }
    }
}
